/***************************************************************************
                          koala.c  -  description
                          ---------------------
    copyright            : (C) 2007 Wolfgang Schulz
    email                : hagenx@gmx.de

 ***************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "koala.h"

// Multicolor
#define LINES_PER_CHAR						   8
#define COLS_PER_CHAR						   4
#define BYTE_PER_CHAR						   8
#define CHAR_PER_ROW						  40
#define BYTE_PER_ROW (BYTE_PER_CHAR*CHAR_PER_ROW)
#define IMG_HEIGHT                           200
#define IMG_WIDTH                            160

// ---------------------------------------------
// 160 * 200 = 32000, 2bit/Pixel,
// i.e. 4 Pixel/Byte ==> 32000/4 = 8000 = 0x1f40
// ---------------------------------------------
#define SIZE_BITMAP      0x1f40 // 8000
#define SIZE_SCREENRAM   0x3e8  // 1000
#define SIZE_COLRAM      0x3e8  // 1000

koalaimage loadkoala(char* filename)
{
	FILE* fp;

	int i,j;
	unsigned char* uc_buf;

	koalaimage img;
	char pixel[4];
	char _row;
	char _char;
	unsigned char x4,y;

	fp = fopen(filename,"rb"); // open as binary
    if (!fp) {
        printf("error opening %s - exiting\n",filename);
        exit(EXIT_FAILURE);
    }
	printf("loaded file: %s\n", filename);

	img.height = IMG_HEIGHT;
	img.width = IMG_WIDTH;

	uc_buf = (unsigned char*)calloc(255, sizeof(unsigned char));

	fread((char*)uc_buf,sizeof(char),2,fp);

	img.bitmap = (unsigned char*)calloc(SIZE_BITMAP,sizeof(unsigned char));
	fread((char*)img.bitmap,sizeof(char),SIZE_BITMAP,fp);

	img.screenram = (unsigned char*)calloc(SIZE_SCREENRAM,sizeof(unsigned char));
	fread((char*)img.screenram,sizeof(char),SIZE_SCREENRAM,fp);

	img.colorram = (unsigned char*)calloc(SIZE_COLRAM,sizeof(unsigned char));
	fread((char*)img.colorram,sizeof(char),SIZE_COLRAM,fp);

	fread((char*)img.background,sizeof(char),1,fp);

//			|  byte0
//			|  byte1		... s. C64 Grafikbuch, Data Becker
//			|  byte2
//	char	|  byte3
//			|  byte4
//			|  byte5
//			|  byte6
//			|  byte7
//           ---------
//             char

	// create memory for iff-image
	img.data = (int**)malloc(img.height * sizeof(int*));
	for (y=0; y < img.height; y++)
	{
		img.data[y] = (int*)calloc(img.width, sizeof(int));
	}

	for (i=0; i < SIZE_BITMAP; i++)
	{
		// ---------------------
		// Multicolor image
		// = 25 rows * 320 bytes
		// ---------------------
		_row = i / BYTE_PER_ROW;
		_char = (i - _row*BYTE_PER_ROW) / BYTE_PER_CHAR;

		y = (_row*LINES_PER_CHAR)+(i%LINES_PER_CHAR);
		x4 = _char*COLS_PER_CHAR;

		pixel[0] = (img.bitmap[i] >> 6) & 3; // 3 = %000011;
		pixel[1] = (img.bitmap[i] >> 4) & 3;
		pixel[2] = (img.bitmap[i] >> 2) & 3;
		pixel[3] =  img.bitmap[i]       & 3;

		for (j=0; j < 4; j++)
		{
			// 00 - background color
			if (pixel[j] == 0) img.data[y][x4+j] = img.background[0];
			// 01 - color of bit 4-7 from Scr-Mem
			if (pixel[j] == 1) img.data[y][x4+j] = c64_color[((img.screenram[_row*CHAR_PER_ROW+_char]>>4) & 0xf)];
			// 10 - color of bit 0-3 from Scr-Mem
			if (pixel[j] == 2) img.data[y][x4+j] = c64_color[((img.screenram[_row*CHAR_PER_ROW+_char]) & 0xf)];
			// 11 - color from nybble (bit 0-3) of Col-Mem
			if (pixel[j] == 3) img.data[y][x4+j] = c64_color[((img.colorram[_row*CHAR_PER_ROW+_char]) & 0xf)];
		}
	}

	free(img.bitmap);
	free(img.colorram);
	free(img.screenram);

	free(uc_buf);
	fclose(fp);

	return img;
}

void freekoala(koalaimage img)
{
	int i;

	for (i=0; i < img.height; i++)
	{
		free(img.data[i]);
	}
	free(img.data);
}
